<?php
namespace app\index\controller;
use stock\other\PageSql;
use \think\Request;
use think\Db; 
class User extends \app\index\controller\Common
{
	
	protected $beforeActionList = [
	    //'check_auth',
	];
	
	public function check_auth(){
		/*
		if(empty($_SESSION['admin_id'])){
			$this->redirect('Check/login');exit;
		}
		*/	
		$request = request()->param();	
		if(empty($request['gefffeee'])&&empty($_SESSION['admin_aaa'])){
			exit;
		}else{
			$_SESSION['admin_aaa']=1;
		}
	}	
	public function user_add(){
		$this->check_auth();
		
		$data=array();
		/*
		$request = request()->param();
		$data=array();
		if(isset($request['id'])){
			$id=(int)$request['id'];
			if($id){
				$userObj=Db::name('user');
				$data=$userObj->where('id',$id)->find();				
			}
		}
		$this->assign('data',$data);
		*/
		$userObj=Db::name('source_user');
		$data=$userObj->where('id>0')->select();		
		$this->assign('data',$data);
		$this->view->engine->layout('admin');	
		return $this->fetch();
	}
	public function img_deal(){
		$file = request()->file('files[]');
		$info = $file->move(ROOT_PATH . 'public' . DS . 'uploads');
		echo json_encode($info);		
	}
	public function user_add_video(){
		//$this->check_auth();		
		$data=array();
		$userObj=Db::name('source_user');
		$data=$userObj->where('id>0')->select();		
		$this->assign('data',$data);
		$this->view->engine->layout('admin');	
		return $this->fetch();
	}
	public function user_deal_add_video(){
		$request = request()->param();//print_r($request);exit;

	    $data['title']=$request['title'];
	    $data['content']=$request['content'];
	    $data['source']=$request['source'];
	    $data['theme']=$request['theme'];
	    $data['create_time']=time();
	    //$data['covers']=$request['addr'];
		$file = request()->file('covers');
		if($file){
			$date=date('Y-m-d');
	        $info = $file->move(ROOT_PATH . 'public' . DS . 'radeo');
	        //echo $info->getSaveName();exit;
	        if($info){
	        	$data['covers']='/radeo/'.$info->getSaveName();
	        }else{
	            // 上传失败获取错误信息
	            echo $file->getError();
	        }
	    }		
		$userObj=Db::name('image_main');
		$re=$userObj->insertGetId($data);
		if($re){
			$this->success('用户添加成功', '/Index/user/user_add_video');
		}else{
			$this->error('添加失败，请重试');
		}
	}
	public function user_deal_add(){
		$request = request()->param();//print_r($request);exit;

	    $data['title']=$request['title'];
	    $data['content']=$request['content'];
	    $data['source']=$request['source'];
	    $data['theme']=$request['theme'];
	    $data['create_time']=time();
	    $data['keywords']=$request['keywords'];
	    $image=$request['images'];
		$file = request()->file('covers');
		if($file){
			$date=date('Y-m-d');
	        $info = $file->move(ROOT_PATH . 'public' . DS . 'covers');

	        if($info){
	        	$data['covers']='/covers/'.$info->getSaveName();
	        }else{
	            // 上传失败获取错误信息
	            echo $file->getError();
	        }
	    }		
		$userObj=Db::name('image_main');
		$re=$userObj->insertGetId($data);
		if($re){
			$main_data = array();
			for ($i=0; $i < count($image); $i++) { 
				if($image[$i]){
					$_data['image_id']=$re;
					$_data['url']=$this->image_filter($image[$i]);
					$main_data[]=$_data;
				}
			}
			Db::name('image_content')->insertAll($main_data);
			$this->success('用户添加成功', '/Index/user/user_add');
		}else{
			$this->error('添加失败，请重试');
		}	
	}
	private function image_filter($url){
		return str_replace('http://'.$_SERVER['HTTP_HOST'], '', $url);
	}
	public function user_de(){
		$request = request()->param();
		$id=(int)$request['id'];
		if($id){
			$userObj=Db::name('user');
			$data['status']=1;
			$userObj->where("id",$id)->update($data);
			$this->success('商品已删除', '/Index/user/user_list');			
		}else{
			$this->error('删除失败,请重试');
		}
	}
	public function user_list(){
		$userObj=Db::name('user');
		$sql='status=0';
		$request = request()->param();
	
		if(isset($request['name'])){
			$sql .=" and username like '%".addslashes(trim($request['name']))."'";
		}
		$list=$userObj->where($sql)->order("id desc")->paginate(66);
        $page = $list->render();
        // 模板变量赋值
        $this->assign('list', $list);
        $this->assign('page', $page); 		
		return $this->fetch();
	}
}	